package cn.zhang.reimbursement_1.service.impl;

import cn.hutool.log.Log;
import cn.zhang.reimbursement_1.common.CommonMethod;
import cn.zhang.reimbursement_1.config.LoginInterceptor;
import cn.zhang.reimbursement_1.request.LoginRequest;
import cn.zhang.reimbursement_1.tools.JsonData;
import cn.zhang.reimbursement_1.vo.RecordVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhang.reimbursement_1.model.Record;
import cn.zhang.reimbursement_1.service.RecordService;
import cn.zhang.reimbursement_1.mapper.RecordMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author zfs90
* @description 针对表【t_record】的数据库操作Service实现
* @createDate 2022-03-25 15:33:41
*/
@Service
public class RecordServiceImpl implements RecordService{

    @Autowired
    private RecordMapper recordMapper;


    /**
     * 分页查询日志
     * @param pageNum
     * @param pageSize
     * @param like
     * @return
     */
    @Override
    public JsonData pageGetRecord(int pageNum, int pageSize, String like) {
        LoginRequest loginRequest = LoginInterceptor.loginThreadLocal.get();
        Page<Record> recordPage = recordMapper.selectPage(new Page<>(pageNum,pageSize)
                , Wrappers.<Record>lambdaQuery()
                        .like(Record::getRealname,like).or()
                        .like(Record::getOperation,like).orderByDesc(Record::getInsertTime));
        Map<String,Object> map = new HashMap<>(3);
        map.put("total_pages",recordPage.getPages());
        map.put("total_record",recordPage.getTotal());
        map.put("records_data",recordPage.getRecords().stream().map(obj->{
            RecordVO recordVO = new RecordVO();
            BeanUtils.copyProperties(obj,recordVO);
            return  recordVO;

        }).collect(Collectors.toList()));
        if (CommonMethod.checkRole(loginRequest)){
            return JsonData.buildSuccess(map);
        }
        return JsonData.buildCodeAndMsg(-1,"权限不足");
    }
}




